package com.bulemobi.activity.ac_login;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.bulemobi.activity.lock.pattern.CreateGesturePasswordActivity;
import com.google.gson.Gson;
import com.bulemobi.bean.UserBean;
import com.bulemobi.data.DataCentre;
import com.bulemobi.net.tsz.afinal.FinalHttp;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.net.tsz.afinal.http.AjaxCallBack;
import com.bulemobi.net.tsz.afinal.http.AjaxParams;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.util.VerifyUtil;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;

/**
 * Created by 武 on 2014/9/16 0016.
 * 用户注册
 */
public class Ac_Register extends BaseActivity implements View.OnClickListener{
    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;
    //昵称
    @ViewInject(id = R.id.ac_register_userName)
    EditText et_userName;
    //手机号码输入框
    @ViewInject(id = R.id.ac_register_userPhone)
    EditText et_userPhone;
    //邮箱输入框
    @ViewInject(id = R.id.ac_register_email)
    EditText et_email;
    //密码输入框
    @ViewInject(id = R.id.ac_register_inputPassWord)
    EditText et_passWord;
    //推荐人手机
    @ViewInject(id = R.id.ac_register_recommendPhone)
    EditText et_rePhone;
    //注册按钮
    @ViewInject(id = R.id.ac_register_registerBt)
    TextView tv_reBt;
    //验证码输入框
    @ViewInject(id = R.id.ac_register_verification)
    EditText et_verfication;
    //获取验证码
    @ViewInject(id = R.id.getVer)
    Button getVer;

    String MobilePhone1;

    TextWatcher watcher=new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if(TextUtils.isEmpty(s.toString().trim())){
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.gray_shape);
                return;
            }else
            if(et_userName.getText().length()<=1 || et_userName.equals("")){
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.gray_shape);

                return;
            }else if(!VerifyUtil.isMobileNO(et_userPhone.getText().toString().trim())){
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.gray_shape);
                return;

            }else if(et_verfication.getText().length()<=0){
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.gray_shape);
                return;

            }else if(et_passWord.getText().length()<8){
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.gray_shape);
                return;
            }else if(!VerifyUtil.is_Email(et_email.getText().toString())){
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.gray_shape);
                return;

            }else if(!VerifyUtil.isMobileNO(et_rePhone.getText().toString())){
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.gray_shape);
                return;
            }else{
                tv_reBt.setClickable(true);
                tv_reBt.setBackgroundResource(R.drawable.blue_shape);
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_user_register);
        findActionBar();
        initView();
    }

    private void findActionBar(){
        actionBarView.title.setText(getResources().getText(R.string.register));
    }

    private void initView(){
        getVer.setOnClickListener(this);
        tv_reBt.setOnClickListener(this);

        //昵称
        et_userName.addTextChangedListener(watcher);
        //手机号码输入框
        et_userPhone.addTextChangedListener(watcher);
        //邮箱输入框
        et_email.addTextChangedListener(watcher);
        //密码输入框
        et_passWord.addTextChangedListener(watcher);
        //推荐人手机
        et_rePhone.addTextChangedListener(watcher);
        //注册按钮
        tv_reBt.addTextChangedListener(watcher);
        //验证码输入框
        et_verfication.addTextChangedListener(watcher);
        tv_reBt.setBackgroundResource(R.drawable.gray_shape);
        tv_reBt.setClickable(false);
    }

    private void requestHttp(String url,AjaxParams params, final String type){
        FinalHttp http = new FinalHttp();
        http.post(url,params,new AjaxCallBack<Object>() {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                closeProgressDialog();
                Log.d("onFailure",strMsg);
                super.onFailure(t, errorNo, strMsg);
            }

            @Override
            public void onStart() {
                showProgressDialog();
                super.onStart();
            }

            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);
            }

            @Override
            public void onSuccess(Object o) {
                closeProgressDialog();
                Log.d("OnSuccess",o.toString());
                if(type.equals(DataCentre.VER)){

                }else if(type.equals(DataCentre.REG)){
                    Gson gson = new Gson();
                    UserBean userBean = gson.fromJson(o.toString(), UserBean.class);
                    SharedPreferences sharedPreferences = getSharedPreferences("phone",0);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("phone",MobilePhone1);
                    editor.commit();
                    openActivity(CreateGesturePasswordActivity.class);
                }
                super.onSuccess(o);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.getVer:
                String MobilePhone = et_userPhone.getText().toString();
                AjaxParams params = new AjaxParams();
                params.put("TerminalType",UrlCentre.TerminalType);
                params.put("UserType",UrlCentre.UserType);
                params.put("ChannelId",UrlCentre.ChannelId);
                params.put("CheckType",UrlCentre.CHECKTYPE);
                params.put("Type","0");
                params.put("MsgId",MobilePhone);
                requestHttp(UrlCentre.GetSMSTokenPublic, params,DataCentre.VER);
                break;
            case R.id.ac_register_registerBt:
                //呢称
                if(et_userName.getText().length()<=1 || et_userName.equals("")){

                    showToastShort("姓名输入有误，请重新输入");
                    return;

                }else if(!VerifyUtil.isMobileNO(et_userPhone.getText().toString().trim())){
                    showToastShort("手机号码格式不正确");
                    return;

                }else if(et_verfication.getText().length()<=0){
                    showToastShort("短信验证码不能为空");
                    return;

                }else if(et_passWord.getText().length()<8){
                    showToastShort("请输入8-16位数字和字母组合");
                    return;
                }else if(!VerifyUtil.is_Email(et_email.getText().toString())){
                    showToastShort("请输入邮箱格式不正确");
                    return;

                }else if(!VerifyUtil.isMobileNO(et_rePhone.getText().toString())){
                    showToastShort("推荐人手机号码格式不正确");
                    return;
                }

                params = new AjaxParams();
                MobilePhone1 = et_userPhone.getText().toString();
                String MSGTokenName = et_verfication.getText().toString();
                String UserId = et_userName.getText().toString();
                String Email = et_email.getText().toString();
                String Password = et_passWord.getText().toString();
                String RecommerndMobile = et_rePhone.getText().toString();
                params.put("TerminalType",UrlCentre.TerminalType);
                params.put("UserType",UrlCentre.UserType);
                params.put("ChannelId",UrlCentre.ChannelId);
                params.put("CheckType",UrlCentre.CHECKTYPE);
                params.put("MobilePhone",MobilePhone1);
                params.put("MSGTokenName",MSGTokenName);
                params.put("UserId",UserId);
                params.put("Email",Email);
                params.put("Password",Password);
                params.put("BankId", DataCentre.BankId);
//                params.put("ConfirmPwd","wu123456");
                params.put("RecommerndMobile",RecommerndMobile);
                Log.d("requestCode",MobilePhone1+"  "+MSGTokenName+"  "+UserId+"  "+Email+
                        "  "+Password+"  "+RecommerndMobile);
                requestHttp(UrlCentre.USERREGIST, params,DataCentre.REG);

                break;
        }
    }
}
